<?php
namespace app\tools\logic;
use think\facade\Db;

class ChannelKeyLogic{
	static function getList(){
		$channel_id = input('channel_id', 0);
		$page_size = input('page_size', 10);
		$list = Db::name('channel_key')
				->alias('c')
				->field('c.*')
				->where('c.status','<>',127)
				->where('channel_id', $channel_id)
				->order('c.id desc')
				->paginate($page_size);
		return $list;
	}

	static function getInfo(){
		$id = input('id');
		if(!$id){
			return '';
		}
		$info = Db::name('channel_key')
				->alias('c')
				->field('c.*')
				->where('c.status','<>',127)
				->where('c.id', $id)
				->find();
		return $info;

	}

	static function save(){
		$id = input('id');
		if(!$id){
			return 110;
		}
		$data = [];
		$data['channel_id'] = input('channel_id','');
		$data['status'] = input('status', 1);
		$data['key'] = input('key', "");

		$rs = Db::name('channel_key')
				->where('id', $id)
				->update($data);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}

	static function add(){
		$data = [];
		$data['channel_id'] = input('channel_id','');
		$data['status'] = input('status', 1);
		$data['key'] = input('key', "");
		$data['create_time'] = date('Y-m-d H:i:s');

		$rs = Db::name('channel_key')->insert($data);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}

	//删除
	static function delete(){
		$id = input('id','');
		$ids = explode(',', $id);
		if(!$ids || !$id){
			return 110;
		}
		$rs = Db::name('channel_key')
				->where('id','in', $ids)
				->update(['status'=>127]);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}
}